<?php

namespace addons\yunfood\model;

use addons\yunfood\library\ModelTrait;
use app\common\model\User;
use think\Model;
use traits\model\SoftDelete;

/**
 * 订单退款模型
 */
class OrderRefund extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_order_refund';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 用户数据
    public function userData()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    // 订单数据
    public function orderData()
    {
        return $this->hasOne(Order::class, 'order_no', 'order_no');
    }

    // 订单商品数据
    public function orderItemData()
    {
        return $this->hasOne(OrderItem::class, 'order_item_no', 'order_item_no');
    }

    // 类别
    const TYPE_ORDER      = 'order';
    const TYPE_ORDER_ITEM = 'order_item';

    public static function getTypeList()
    {
        return [self::TYPE_ORDER => __('整个订单'), self::TYPE_ORDER_ITEM => __('部分商品')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $optionList = self::getTypeList();

        return $optionList[$data['type']] ?? $data['type'];
    }

    // 退款原因
    const REASON_CHAT  = 'chat';
    const REASON_OTHER = 'other';

    public static function getReasonList()
    {
        return [self::REASON_CHAT => __('沟通协商'), self::REASON_OTHER => __('其他')];
    }

    public function getReasonTextAttr($value, $data)
    {
        $optionList = self::getReasonList();

        return $optionList[$data['reason']] ?? $data['reason'];
    }

    // 退款方式
    const REFUND_WAY_OFFLINE = 'offline';

    public static function getRefundWayList()
    {
        return [self::REFUND_WAY_OFFLINE => __('线下转账')];
    }

    public function getRefundWayTextAttr($value, $data)
    {
        $optionList = self::getRefundWayList();

        return $optionList[$data['refund_way']] ?? $data['refund_way'];
    }

    // 退款图片列表
    public function getRefundPhotoListAttr($value, $data)
    {
        $photoList = [];
        if ($photos = $data['refund_photos']) {
            $photos = explode(',', $photos);
            foreach ($photos as $item) {
                $photoList[] = ['src' => $item, 'url' => cdnurl($item, true)];
            }
        }

        return $photoList;
    }

    // 状态
    const STATUS_WAIT   = 1;
    const STATUS_DONE   = 2;
    const STATUS_CANCEL = 3;
    const STATUS_REFUSE = 4;

    public static function getStatusList()
    {
        return [
            self::STATUS_WAIT   => __('待处理'),
            self::STATUS_DONE   => __('已退款'),
            self::STATUS_CANCEL => __('已取消'),
            self::STATUS_REFUSE => __('已拒绝')
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }
}
